package com.eas.biz.service.user.impl;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eas.biz.framework.cache.CacheConfig;
import com.eas.biz.framework.cache.CacheProvider;
import com.eas.biz.framework.cache.CacheStore;
import com.eas.biz.service.user.domain.ClassInfo;
import com.eas.biz.service.user.interfaces.ClassService;

@Service("classCacheService")
public class ClassCacheServiceImpl extends ClassServiceImpl implements ClassService {

    @Autowired
    private CacheProvider cacheProvider;

    private CacheStore cacheStore;

    @PostConstruct
    public void init() {
        CacheConfig config = new CacheConfig();
        config.setName("classInfo");
        config.setRefreshSeconds(24 * 60 * 60);
        config.setCapacity(100);
        cacheStore = cacheProvider.create(config);
    }

    @Override
    public ClassInfo getClassInfo(long classId) {
        Object result = cacheStore.get(classId);
        if (result != null && result instanceof ClassInfo) {
            return (ClassInfo) result;
        } else {
            ClassInfo classInfo = super.getClassInfo(classId);
            if (classInfo != null) {
                cacheStore.put(classId, classInfo);
            }
            return classInfo;
        }
    }
}
